package stack_and_queue;

import java.util.Stack;

/**
 * @author: Zhenzhou Liu
 * @create: 2025-01-10
 * @Description:
 */
public class QueueUsingStack {
    private Stack<Integer> inStack, outStack;

    public QueueUsingStack() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if (outStack.empty())
            in2out();
        int x = outStack.peek();
        outStack.pop();
        return x;
    }

    private void in2out() {
        while (!inStack.empty()) {
            outStack.push(inStack.peek());
            inStack.pop();
        }
    }

    public int peek() {
        if (outStack.empty())
            in2out();
        return outStack.peek();
    }

    public boolean empty() {
        return inStack.empty() && outStack.empty();
    }

}
